package icu.qx.pro.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.domain.ProWorkPeo;
import icu.qx.pro.domain.ProWorkReport;
import icu.qx.pro.domain.pbs.PbsProcedure;
import icu.qx.pro.domain.pbs.PbsProcedureRoute;
import icu.qx.pro.domain.pbs.PbsWorkCell;
import icu.qx.pro.service.IProWorkOrderService;
import icu.qx.pro.service.IProWorkPeoService;
import icu.qx.pro.service.IProWorkReportService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import icu.qx.pro.mapper.ProWorkTaskMapper;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.service.IProWorkTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单任务Service业务层处理
 *
 * @author yong
 * @date 2024-06-24
 */
@Service
public class ProWorkTaskServiceImpl implements IProWorkTaskService {
    @Autowired
    private ProWorkTaskMapper proWorkTaskMapper;
    @Autowired
    private IProWorkPeoService proWorkPeoService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询工单任务
     *
     * @param wtaskCode 工单任务主键
     * @return 工单任务
     */
    @Override
    public ProWorkTask selectProWorkTaskByWtaskCode(String wtaskCode) {
        return proWorkTaskMapper.selectProWorkTaskByWtaskCode(wtaskCode);
    }

    @Override
    public List<ProWorkTask> selectTaskListByOrderCode(String wtaskWoCode) {
        return proWorkTaskMapper.selectTaskListByOrderCode(wtaskWoCode);
    }

    /**
     * 查询工单任务列表
     *
     * @param proWorkTask 工单任务
     * @return 工单任务
     */
    @Override
    public List<ProWorkTask> selectProWorkTaskList(ProWorkTask proWorkTask) {
        return proWorkTaskMapper.selectProWorkTaskList(proWorkTask);
    }

    @Override
    public List<PbsProcedureRoute> selectRouteInTask() {
        return proWorkTaskMapper.selectRouteInTask();
    }

    @Override
    public List<PbsProcedure> selectProcedureInTask() {
        return proWorkTaskMapper.selectProcedureInTask();
    }

    @Override
    public List<PbsWorkCell> selectWorkCellInTask() {
        return proWorkTaskMapper.selectWorkCellInTask();
    }

    @Override
    public List<SysUser> selectTaskPeo(String wtaskCode) {
        return proWorkTaskMapper.selectTaskPeo(wtaskCode);
    }

    /**
     * 新增工单任务
     *
     * @param proWorkTask 工单任务
     * @return 结果
     */
    @Override
    public int insertProWorkTask(ProWorkTask proWorkTask) {
        proWorkTask.setCreateBy(SecurityUtils.getUsername());
        proWorkTask.setCreateTime(DateUtils.getNowDate());
        return proWorkTaskMapper.insertProWorkTask(proWorkTask);
    }

    /**
     * 修改工单任务
     *
     * @param proWorkTask 工单任务
     * @return 结果
     */
    @Override
    public int updateProWorkTask(ProWorkTask proWorkTask) {
        proWorkTask.setUpdateBy(SecurityUtils.getUsername());
        proWorkTask.setUpdateTime(DateUtils.getNowDate());
        return proWorkTaskMapper.updateProWorkTask(proWorkTask);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int arrangePeo(ProWorkTask proWorkTask, List<ProWorkPeo> proWorkPeo) {
        proWorkPeo.forEach(e -> {
            proWorkPeoService.insertProWorkPeo(e);
        });
        return updateProWorkTask(proWorkTask);
    }

    @Override
    public int startWork(ProWorkTask proWorkTask) {
        proWorkTask.setWtaskRStart(DateUtils.getNowDate());
        // 判断之前工序是否有任务
        ProWorkTask condition = new ProWorkTask();
        condition.setWtaskWoCode(proWorkTask.getWtaskWoCode());
        condition.setWtaskSort(proWorkTask.getWtaskSort());
        condition.setWtaskStatus("1");
        List<ProWorkTask> proWorkTasks = proWorkTaskMapper.selectOtherProWorkTask(condition);
        if (proWorkTasks != null && !proWorkTasks.isEmpty()) {
            return -1;
        }
        // 更新工单
        ProWorkOrder proWorkOrder = new ProWorkOrder();
        proWorkOrder.setWorkOrderCode(proWorkTask.getWtaskWoCode());
        proWorkOrder.setWorkOrderStatus("2");
        rabbitTemplate.convertAndSend("proStatusConsumer.exchange", "changeWorkOrder", proWorkOrder);
        return updateProWorkTask(proWorkTask);
    }

    /**
     * 批量删除工单任务
     *
     * @param wtaskCodes 需要删除的工单任务主键
     * @return 结果
     */
    @Override
    public int deleteProWorkTaskByWtaskCodes(String[] wtaskCodes) {
        return proWorkTaskMapper.deleteProWorkTaskByWtaskCodes(wtaskCodes);
    }

    /**
     * 删除工单任务信息
     *
     * @param wtaskCode 工单任务主键
     * @return 结果
     */
    @Override
    public int deleteProWorkTaskByWtaskCode(String wtaskCode) {
        return proWorkTaskMapper.deleteProWorkTaskByWtaskCode(wtaskCode);
    }
}
